﻿using System.Collections.Generic;
using System.Linq;
using Helpers;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.Core;
using TaleWorlds.Localization;

namespace MarriageFertility.Helpers;

public static class KingdomHelper
{
    public static void DealKingdomLeader(Clan clan, Hero hero)
    {
        if (clan.Kingdom == null || clan.Kingdom.Leader != hero)
            return;
        List<Clan> list = clan.Kingdom.Clans.Where(obj => obj != clan && !obj.IsEliminated).ToList();
        if (list.Count > 0)
        {
            Clan clan1 = list.OrderByDescending(item => item.Renown).First();
            clan.Kingdom.RulingClan = clan1;
            TextObject textObject = new TextObject(
                    "{=sue_more_spouses_kingdom_leader_change}{SUE_HERO.NAME}became the new leader of the country named{SUE_HERO.FACTION}");
            StringHelpers.SetCharacterProperties("SUE_HERO", clan1.Leader.CharacterObject, textObject);
            MBInformationManager.AddQuickInformation(textObject, soundEventPath: "event:/ui/notification/quest_finished");
        }
        else
            DestroyKingdomAction.Apply(clan.Kingdom);
    }
}